package com.gs.m_index

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import com.gs.common.BaseActivity
import com.gs.common.constant.ActionConst
import com.gs.common.global.GlobalVariables
import com.gs.common.sqlite.UserSqliteOpenHelper
import com.gs.common.util.ToastUtil
import com.gs.m_index.databinding.IndexLayoutBinding
import com.gs.m_login.LoginModel

class IndexActivity: BaseActivity(), IndexPersistent.OnDoAutoLoginStateCallback {

    private lateinit var binding: IndexLayoutBinding
    private val indexPersistent by lazy { IndexPersistent.getInstance(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = IndexLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 使用上一个账号自动登录
        autoLogin()
    }

    private fun autoLogin() {
        indexPersistent.autoLogin(this)
    }

    fun toLogin(view: View) {
        val intent = Intent().apply {
            action = ActionConst.TO_LOGIN_ACTIVITY_ACTION
        }
        startActivity(intent)
    }

    fun toRegister(view: View) {
        val intent = Intent().apply {
            action = ActionConst.TO_REGISTER_ACTIVITY_ACTION
        }
        startActivity(intent)
    }

    override fun onAutoLoginSuccess() {
        ToastUtil.showShortToast(this, "自动登录成功！")
        val intent = Intent().apply {
            action = ActionConst.TO_HOME_ACTIVITY_ACTION
        }
        startActivity(intent)
        this.finish()
    }

    override fun onAutoLoginFailed() {
        ToastUtil.showShortToast(this, "自动登录失败！请手动登录试试！")
    }

    override fun onNetworkError() {
        ToastUtil.showShortToast(this, "网络连接失败！请检查网络后，手动登录！")
    }

    override fun onServerError() {
        ToastUtil.showShortToast(this, "服务器连接失败！")
    }
}